/*
 * 类名称:AccountWithdrawServiceImpl.java
 * 包名称:com.unimall.graded.app.api.accountwithdraw
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-03-17 09:48:28        lin     初版做成
 *
 * Copyright (c) 2019-2019 浵浵科技
 */
package com.unimall.graded.app.api.accountwithdraw;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.unimall.graded.data.domain.AccountWithdrawDO;
import com.unimall.graded.data.mapper.AccountWithdrawMapper;
import com.iotechn.unimall.data.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.apache.ibatis.session.RowBounds;

import java.util.Date;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 提现表ServiceImpl实现类
 *
 * @author lin
 * @date 2020-03-17 09:48:28
 */
@Service
public class AccountWithdrawServiceImpl implements AccountWithdrawService {

    @Autowired
    private AccountWithdrawMapper accountWithdrawMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addAccountWithdraw(AccountWithdrawDO accountWithdrawDO) throws ServiceException{
        if (accountWithdrawDO == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
          if( accountWithdrawDO.getUserId() == null ){
            throw new AdminServiceException("unimall的用户id不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
         if( StringUtils.isEmpty(accountWithdrawDO.getAmount()) ){
            throw new AdminServiceException("提现金额不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
      
        Date now = new Date();
        accountWithdrawDO.setId(null);
        accountWithdrawDO.setGmtCreate(now);
        accountWithdrawDO.setGmtUpdate(now);
        return accountWithdrawMapper.insert(accountWithdrawDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteAccountWithdraw(Long userId,Long id) throws ServiceException{
        return accountWithdrawMapper.delete(new EntityWrapper<AccountWithdrawDO>()
                .eq("id", id)) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAccountWithdraw(Long userId,AccountWithdrawDO accountWithdrawDO) throws ServiceException{
        if (accountWithdrawDO == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
               if( accountWithdrawDO.getUserId() == null ){
                throw new AdminServiceException("unimall的用户id不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
            }
              if( StringUtils.isEmpty(accountWithdrawDO.getAmount()) ){
                throw new AdminServiceException("提现金额不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
            }
          
        Date now = new Date();
        accountWithdrawDO.setGmtUpdate(now);
        return accountWithdrawMapper.updateById(accountWithdrawDO) > 0;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public AccountWithdrawDO getAccountWithdrawById(Long userId,Long id) throws ServiceException{

        if(id == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }

        return accountWithdrawMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<AccountWithdrawDO> getAccountWithdrawList(
                                             Long id,
                                                                                String amount,
                                                         String channel,
                                                                                                                              Long userId
                    ) throws ServiceException{

        Wrapper<AccountWithdrawDO> wrapper = new EntityWrapper<AccountWithdrawDO>();
        wrapper.orderBy("id", false);

                    if( id != null ){
                wrapper.eq("id", id);
            }
                    if( userId != null ){
                wrapper.eq("user_id", userId);
            }
                    if( !StringUtils.isEmpty(amount) ){
                wrapper.eq("amount", amount);
            }
                    if( !StringUtils.isEmpty(channel) ){
                wrapper.eq("channel", channel);
            }
                                                                                                            
        List<AccountWithdrawDO> AccountWithdrawDOs = accountWithdrawMapper.selectList(wrapper);
        return AccountWithdrawDOs;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page<AccountWithdrawDO> getAccountWithdrawListWithPage(
           Long userId,
            Long id,
                    String amount,
                String channel,
     Integer pageNo,Integer limit) throws ServiceException{

        Wrapper<AccountWithdrawDO> wrapper = new EntityWrapper<AccountWithdrawDO>();
        wrapper.orderBy("id", false);

    if( id != null ){
            wrapper.eq("id", id);
        }
        if( userId != null ){
            wrapper.eq("user_id", userId);
        }
        if( !StringUtils.isEmpty(amount) ){
            wrapper.eq("amount", amount);
        }
        if( !StringUtils.isEmpty(channel) ){
            wrapper.eq("channel", channel);
        }
                        
        List<AccountWithdrawDO> AccountWithdrawDOs = accountWithdrawMapper.selectPage(new RowBounds((pageNo - 1) * limit, limit), wrapper);
        Integer count = accountWithdrawMapper.selectCount(wrapper);
        return new Page<AccountWithdrawDO>(AccountWithdrawDOs, pageNo, limit, count);
    }
}